<?php 
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $con = mysql_connect("localhost", "root", "root");
        if (!$con) {
            die("couldn't connect " . mysql_error());
        }
        mysql_select_db("jianglaifarm", $con);
        $result = mysql_query("select count(id) as num from t_user where login_id = '$_POST[loginId]'");
        $flag = false;
        while($row = mysql_fetch_array($result)){
            if($row['num']>0){
                $flag = true;
            }
        }
        if($flag){
            $flag = false;
            $isAdmin = false;
            $result = mysql_query("select id,login_id,user_name,count(id) as num from t_user where login_id = '$_POST[loginId]' and password='$_POST[password]'");
            while($row = mysql_fetch_array($result)){
                if($row['num']>0){
                    session_start();
                    $_SESSION['userId']=$row['id'];
                    $_SESSION['userName']=$row['user_name'];
                    $flag = true;
                    $isAdmin = ($row['login_id']=="admin");
                }
            }
            if($flag){
                if($isAdmin){
                    header("Location:adminMain.php");
                }else{
                    header("Location:main.php");
                }
                exit();
            }else{
                echo "用户名或密码错误";
            }
        }else{
            echo "用户名不存在";
        }
    }

?>

<!DOCTYPE html>
<html>
    <head>
        <title>login</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>登录</div>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
            用户名<input type="text" name="loginId" /><br/>
            密码  <input type="password" name="password"/><br/>
            <input type="submit" name="submit" value="提交"><a href="regist.html">注册</a>
        </form>
    </body>
</html>